/*
 * Package il.ac.biu.cs.grossmm.test.SimpleProtocol
 * File NotifyRequest.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.foo.protocol;

import il.ac.biu.cs.grossmm.foo.format.PresenceDoc;
import il.ac.biu.cs.grossmm.foo.format.WatcherRulesDoc;

public class NotifyRequest extends Request {
	String sid;

	boolean isSuccess;
	
	String watcher;

	int duration;

	String payloadType;

	String payload;

	/**
	 * @param sid
	 * @param isSuccess
	 * @param payloadType
	 * @param payload
	 */
	public NotifyRequest(String sid, String watcher, boolean isSuccess, int duration,
		String payloadType, String payload)
	{
		super();
		this.sid = sid;
		this.watcher = watcher;
		this.isSuccess = isSuccess;
		this.duration = duration;
		this.payloadType = payloadType;
		this.payload = payload;
	}

	public NotifyRequest(String tid, String[] params)
	{
		super(tid);
		this.sid = params[0];
		this.watcher = params[1];
		this.isSuccess = params[2].equals("SUCCESS");
		this.duration = Integer.parseInt(params[3]);
		this.payloadType = params[4];
		this.payload = params[5];
	}

	@Override
	public String getCommand()
	{
		return "NOTIFY";
	}

	@Override
	public String[] getParameters()
	{
		return new String[] { sid, watcher, isSuccess ? "SUCCESS" : "FAILURE",
			Integer.toString(duration), payloadType, payload };
	}

	public static void understand()
	{
		RequestFactory fac = new RequestFactory() {
			public Request getRequest(String tid, String[] params)
			{
				return new NotifyRequest(tid, params);
			}
		};

		factories.put("NOTIFY", fac);
	}

	public int getDuration()
	{
		return duration;
	}

	public boolean isSuccess()
	{
		return isSuccess;
	}

	public String getPayload()
	{
		return payload;
	}

	public String getPayloadType()
	{
		return payloadType;
	}

	public String getSid()
	{
		return sid;
	}

	public String getWatcher()
	{
		return watcher;
	}

	@Override
	public String toString()
	{
		StringBuffer sb = new StringBuffer("NOTIFY ");
		sb.append(sid);
		sb.append(' ').append(watcher);
		sb.append(' ').append(duration).append(' ');
		
		if( isSuccess) {
			sb.append("success ");
		
			if( payloadType.equals("foo.PresenceDoc")) {
				sb.append('\n').append(new PresenceDoc(payload));
			} else if( payloadType.equals("foo.WatcherRulesDoc")) {
				sb.append('\n').append(new WatcherRulesDoc(payload));
			} else
				sb.append(payloadType).append(" (payload)");
		} else 
			sb.append("failure");
		
		return sb.toString();
	}
}
